﻿using GFluentCode.Entity;
using GFluentCode.ORM;
using GFluentCode.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GFluentCode.Demo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        #region  文件监控

        private void BtnTestWatcher_Click(object sender, EventArgs e)
        {
            FileWatcher.Init("xml", AppDomain.CurrentDomain.BaseDirectory, "*.xml", false);
            FileWatcher.Init("txt", AppDomain.CurrentDomain.BaseDirectory, "*.txt", false);

            FileWatcher.FileChanged += (wacther, name, fullPath) =>
            {
                MessageBox.Show(name);
            };
        }

        private void BtnStopWatch_Click(object sender, EventArgs e)
        {
            FileWatcher.StopWatch("txt");
        }

        #endregion

        #region XML读写

        string xmlTestPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config2.xml");

        private void BtnXml_Click(object sender, EventArgs e)
        {
            var list = new List<DbModel>
            {
                new DbModel{DbAddr="1",DbName="1",UserName="1",Password=DescEncryptHelper.Encrypt("1","AsakuraY")},
                new DbModel{DbAddr="2",DbName="2",UserName="2",Password=DescEncryptHelper.Encrypt("2","AsakuraY")},
                new DbModel{DbAddr="3",DbName="3",UserName="3",Password=DescEncryptHelper.Encrypt("3","AsakuraY")}
            };

            XmlHelper.Save<DbModel>(xmlTestPath, "config", "items", list);
        }

        private void BtnReadXml_Click(object sender, EventArgs e)
        {
            var list = XmlHelper.ReadList<DbModel>(xmlTestPath, "items");
        }

        #endregion

        #region 验证码
        private void BtnRandomCode_Click(object sender, EventArgs e)
        {
            var code = RandomKeyHelper.GenerateRandomNumber(8);
            MessageBox.Show(code);
        }

        private void BtnRandomCode2_Click(object sender, EventArgs e)
        {
            var code = RandomKeyHelper.GenerateRandomNumber2(4);
            MessageBox.Show(code);
        }
        #endregion

        private void BtnSocket_Click(object sender, EventArgs e)
        {
            GDatePair a = new GDatePair();
            var b = a == null;

            var frm = new FrmSocket();
            frm.ShowDialog();
        }

        #region 测试
        class UserPurviewEntity: GFluentEntityBase
        {
            public int EnTelBookRange { get; set; }
            public string UserPurview { get; set; }
            public List<Module> UserPurviewList { get; set; }
            public string RolePurview { get; set; }
            public List<Module> RolePurviewList { get; set; }
            public string TotalPurview { get; set; }
            public List<Module> TotalPurviewList { get; set; }
        }
        class Module
        {
            public string ModuleID { get; set; }
            public string ModuleName { get; set; }
            public string ModuleUrl { get; set; }
            public string ModuleIcon { get; set; }
        }
        class UserPurviewORM : GFluentORMBase<UserPurviewEntity>
        {
            public UserPurviewORM(string conn) : base(conn, EnDbType.SqlServer) { }

            public UserPurviewEntity Get(string userId,string orgId)
            {                
                var tmp = DBaser.StoredProcedure("GetPurview")
                    .Parameter("UserID", userId).Parameter("OrgID", orgId)
                    //.QuerySingle<dynamic>();
                    .QueryComplexSingle(ComplexEntity);
                return tmp;
            }
            UserPurviewEntity ComplexEntity(FluentData.IDataReader reader)
            {
                try
                {
                    var db = reader as FluentData.IDataReader;
                    if (db == null) return null;
                    var r = new UserPurviewEntity();
                    r.EnTelBookRange = db.GetInt32("EnTelBookRange");
                    r.UserPurviewList = db.GetString("UserPurview").GIsEmpty(true) ? null :
                        Newtonsoft.Json.JsonConvert.DeserializeObject<List<Module>>(db.GetString("UserPurview"));
                    r.RolePurviewList = db.GetString("RolePurview").GIsEmpty(true) ? null :
                        Newtonsoft.Json.JsonConvert.DeserializeObject<List<Module>>(db.GetString("RolePurview"));
                    r.TotalPurviewList = db.GetString("TotalPurview").GIsEmpty(true) ? null :
                        Newtonsoft.Json.JsonConvert.DeserializeObject<List<Module>>(db.GetString("TotalPurview"));
                    //r.PurviewList= db.GetString("Purview").GIsEmpty(true) ? null :
                    //    Newtonsoft.Json.JsonConvert.DeserializeObject<List<Module>>(db.GetString("Purview"));
                    return r;
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        private void BtnTest_Click(object sender, EventArgs e)
        {            
            var conn = $"Data Source=.\\MSSQLSERVER_NEW;Initial Catalog=TestPurview;User ID=sa;password=sasa";

            var orm = new UserPurviewORM(conn);
            var tmp = orm.Get("1", "1");

        }
        #endregion
    }
}
